#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"

#define BP 10

int main( int argc, char *argv[] )
{
	int i, fd;
	if ( argc <= 1 )
	{
		printf( 1, "no argc\n" );
		exit();
	}
	int filenum = atoi( argv[1] );
	if ( filenum > 15 )
	{
		printf( 1, "to mach testfile\n" );
		exit();
	}

	printf( 1, "------test large file write------\n" );

	int	start, end;
	char	str[512];
	for ( i = 0; i < 512; i++ )
	{
		str[i] = 'a';
	}
	char filename[100][3];
	for ( i = 0; i < filenum; i++ )
	{
		filename[i][0]	= i / 10 + '0';
		filename[i][1]	= i % 10 + '0';
	}
  
	// printf( 1, "------make trash file------\n");

	// if ( (fd1 = open( "trash1", O_WRONLY | O_CREATE ) ) < 0 )
	// {
	// 	printf( 1, "test: cannot open %s\n", "trash1" );
	// 	exit();
	// }
	// for ( i = 0; i < 30; i++ )
	// 	write( fd1, str, 512 );

	// if ( (fd2 = open( "trash2", O_WRONLY | O_CREATE ) ) < 0 )
	// {
	// 	printf( 1, "test: cannot open %s\n", "trash2" );
	// 	exit();
	// }
	// for ( i = 0; i < 5; i++ )
	// 	write( fd2, str, 512 );

	// if ( (fd3 = open( "trash3", O_WRONLY | O_CREATE ) ) < 0 )
	// {
	// 	printf( 1, "test: cannot open %s\n", "trash3" );
	// 	exit();
	// }
	// for ( i = 0; i < 20; i++ )
	// 	write( fd3, str, 512 );

	// close( fd1 );
	// close( fd2 );
	// close( fd3 );

	// unlink( "trash2" );

	printf( 1, "------make test file------\n" );


	start = uptime();

	for ( i = 0; i < filenum; i++ )
	{
        if ( (fd = open( filename[i], O_WRONLY | O_CREATE ) ) < 0 )
		{
			printf( 1, "test: cannot open %d: %s\n", i, filename[i] );
			exit();
		}
		for ( int ii = 0; ii < BP; ii++ )
			write( fd, str, 512 );
        close(fd);
	}
	/* sleep(100); */

	end = uptime();

	printf( 1, "write time total: %d, avg: %d\n", end - start, (end - start) / filenum );
  
	exit();
}